let dataList = []
const MAX_SIZE = 20

let totalPrice = 0
let distanceBegin = 0
let distanceEnd = 0
let addressBegin = ''
let addressBeginFinish = false
let addressEnd = ''
let addressEndRegion = ''
let timeBegin = 0
let timeEnd = 0
let map = []
let orderType = ""
let hasShowDistance = false
let hasShowMap = false

function resetData() {
    totalPrice = 0
    distanceBegin = 0
    distanceEnd = 0
    addressBegin = ''
    addressBeginFinish = false
    addressEnd = ''
    addressEndRegion = ''
    timeBegin = 0
    timeEnd = 0
    map = []
    orderType = ""
    hasShowDistance = false
    hasShowMap = false
}

function parse(jsonText, cutX, cutY, scale) {
    const len = jsonText.length
    let orders = []
    for (let i = 0; i < len; i++) { 
        let word = jsonText[i]
        let textLeft = word.bounds.left
        let textTop = word.bounds.top
        let text = word.text
        
        
        // 如果有新的订单，则先刷新页面
        if (text == "您有新的订单") {
            click(545, 275) // 这里直接点击刷新按钮
            continue
        }

        if(text == "没有更多内容了" || text == '正在查找附近订单') break

        // 获取订单类型
        if (orderType == "") {
            // 这里需要注意 四轮小件也有特快
            if (text.indexOf("快车") > -1 || text.indexOf("特快") > -1 ) {
                    orderType = text
            }
            continue 
        }
        
        // 获取总价格
        if (totalPrice == 0 && text.indexOf("元") > -1) {
            totalPrice = Number(text.split("元")[0])
            // console.log("总价格:" + totalPrice)
            continue
        }
       
        // 获取距离起点
        if (distanceBegin == 0 && text.indexOf("公里") > -1) {
            distanceBegin = Number(text.split("公里")[0])
            // console.log("起点距离:" + distanceBegin)
            continue
        }

        if (distanceEnd == 0 && (text.indexOf("公里") > -1 || text.indexOf("公") > -1)) {
            distanceEnd = Number(text.split("公")[0])
            // console.log("终点距离:" + distanceEnd)
            continue
        }

        if (hasShowDistance == false && text == '总里程') {
            hasShowDistance = true
            continue
        }

        if (hasShowMap == false && text.indexOf("地图") > -1) {
            hasShowMap = true
            if (text == "地图>" || text == "地图" || text == "地图》") continue
        }
        // 如果起点名字太长，会引起换行，导致起点名字被截断 并且终点和地图两字可能连在一起
        // 这里的逻辑是 如果和地图连在一起或地图之后(总之显示地图后)，那就是终点

        // 起点，地图，终点可能存在的情况 1：起点+地图+终点 2：起点+终点+地图+终点 3：起点+起点+地图+终点
        if (hasShowDistance && hasShowMap == false && addressBeginFinish == false) {
            addressBegin = addressBegin + tools.trim(text)
            if (addressBegin.length < 15)
                addressBeginFinish = true

            // console.log("起点:" + addressBegin)
            continue
        }

        let lastChar = text.charAt(text.length - 1)
        if (addressEndRegion == '' && "区县市".indexOf(lastChar) > -1 && text.length < 5) {
            addressEndRegion = text
            console.log("终点区域:" + addressEndRegion)

            // 如果区域之前是地图 那么地图之前的地点就是终点
            let beforeText = jsonText[i - 1].text
            if (beforeText == "地图" || beforeText == "地图>" || beforeText == "地图》" ) {
                let beforePosition = jsonText[i - 2].text
                addressEnd = tools.trim(beforePosition)
                addressBegin = addressBegin.replace(addressEnd, "")
            }
            continue
        }


        if ((hasShowMap || addressBeginFinish) && addressEndRegion == '') {
            addressEnd = addressEnd + tools.trim(text).replace("地图>", "").replace("地图", "")
            // console.log("终点:" + addressEnd)
            continue
        }
        

        // 如果订单信息结束了且不是网约车单，复位以上信息
        if(text.indexOf("抢") > -1 && text.indexOf("抢网约车单") == -1) {
            resetData()
            continue
        }

        // 这里是为了截获其他字段信息 只有抢网约车单才能继续执行
        if(text.indexOf("抢网约车单") == -1) 
            continue

        // 单击抢单
        let btnX = cutX + textLeft
        let btnY = cutY + textTop - 100

        console.log("识别出订单【" + orderType + '】:从' + addressBegin + '==>' + addressEndRegion +  addressEnd)
        // 以下是已经完整的识别出一个完整订单
        orders.push(
            { orderType, totalPrice, distanceBegin, distanceEnd, addressBegin, addressEnd, addressEndRegion, btnX, btnY }
        )

        resetData()
    }

    if(orders.length == 0) {
        // console.log('暂无订单')
    }
    return orders
}

function find(newData) {
    // 检查是否存在相同起点和终点的数据
    return dataList.find(item => 
        item.addressBegin === newData.addressBegin && 
        item.addressEnd === newData.addressEnd
    )
}

function add(newData) {
    dataList.push(newData)

    if (dataList.length > MAX_SIZE) {
        dataList.shift()
    }
}


module.exports = {
    find, add, parse
}